//
//  XXGKNet2.m
//  上海杨浦
//
//  Created by FrankChen on 13-5-26.
//  Copyright (c) 2013年 FrankChen. All rights reserved.
//

#import "XXGKNet2.h"

@implementation XXGKNet2

-(NSString *) getHtml:(NSString *)webUrl{
    @autoreleasepool {
        NSURL *url = [NSURL URLWithString:webUrl];
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        NSString *retStr = [[[NSString alloc]init]autorelease];
        retStr=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        if([retStr isEqual:@""]){
            [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];
        }
        
        return retStr;
    }
}

-(void)showAlert{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
}



-(NSString *) getData:(NSString *)html{
    
    NSString *content=[[[NSString alloc]init]autorelease];
    NSString *tag1=[[[NSString alloc]init]autorelease];
    NSString *tag2=[[[NSString alloc]init]autorelease];
    NSString *tag3=[[[NSString alloc]init]autorelease];
    
    content=@"";
    tag1=@"日</td>";
    tag2=@"<SCRIPT";
    tag3=@"表<SPAN";
    
    NSString *tag4=[[[NSString alloc]init]autorelease];
    tag4=@"附件：";
    NSString *tag5=[[[NSString alloc]init]autorelease];
    tag5=@"附件</SPAN";
    NSString *tag6=[[[NSString alloc]init]autorelease];
    tag6=@"附件一：";
    
    NSRange range1=[html rangeOfString:tag1];
    
    content=[html substringFromIndex:(range1.location+range1.length)];

    NSRange range2=[content rangeOfString:tag2];
    content=[content substringToIndex:(range2.location)];
    
    NSRange range3=[content rangeOfString:tag3];
    if (range3.length!=0) {
        content=[content substringToIndex:range3.location];
    }
    NSRange range4=[content rangeOfString:tag4];
    if (range4.length!=0) {
        content=[content substringToIndex:range4.location];
    }
    NSRange range5=[content rangeOfString:tag5];
    if (range5.length!=0) {
        content=[content substringToIndex:range5.location];
    }
    NSRange range6=[content rangeOfString:tag6];
    if (range6.length!=0) {
        content=[content substringToIndex:range6.location];
    }
    
    content=[self filterScript:content];
    
    content=[self filterHtmlTag:content];
    
    content=[self filterKongGeHuiChe:content];
    
    content=[@"  \n" stringByAppendingString:content];
    content=[content stringByAppendingString:@"\n\n                       "];
    
    return content;
    
}

-(NSString *) getData_Xxgknb:(NSString *)html{
    NSString *content=[[[NSString alloc]init]autorelease];
    
    NSString *tag1=[[[NSString alloc]init]autorelease];
    tag1=@"日</td>";
    NSString *tag2=[[[NSString alloc]init]autorelease];
    tag2=@"<SCRIPT";
    NSString *tag3=[[[NSString alloc]init]autorelease];
    tag3=@"附表一";

    
    NSRange range1=[html rangeOfString:tag1];
    content=[html substringFromIndex:(range1.location+range1.length)];
    
    
    NSRange range2=[content rangeOfString:tag2];
    content=[content substringToIndex:(range2.location)];
    
    NSRange range3=[content rangeOfString:tag3];
    content=[content substringToIndex:(range3.location)];
    
    content=[self filterScript:content];
    
    content=[self filterHtmlTag_Xxgknb:content];
    content=[content stringByReplacingOccurrencesOfString:@"\n" withString:@"    "];
    
    content=[self filterKongGeHuiChe:content];
    
    content=[@"  \n" stringByAppendingString:content];
    content=[content stringByAppendingString:@"\n\n                       "];
    
    return content;
    
}

-(NSString *)filterKongGeHuiChe:(NSString *)content{
    NSRange range5;
    NSRange range6;
    Boolean stop=false;
    while (!stop) {      
        range5=[content rangeOfString:@" "];
        range6=[content rangeOfString:@"\n"];
        if(range5.location!=0&&range6.location!=0){
            stop=true;
        }
        else{
            content=[content substringFromIndex:1];
        }
    }
    return content;
}


- (NSString *)filterScript:(NSString *)originHtmlStr{
    NSString *result = [[[NSString alloc]init]autorelease];
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<style type=\"text/css\">"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@"</style>"];
            result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location+8) withString:@""];
        return [self filterScript:result];
    }
    else{
        result=originHtmlStr;
    }
    return result;
}


- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
    NSString *htmlresult = [[[NSString alloc]init]autorelease];
    
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];

        htmlresult = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
        
            return [self filterHtmlTag:htmlresult];
           //递归，过滤下一个标签
    }else{
        htmlresult = [originHtmlStr stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];  // 过滤&nbsp等标签
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........
        htmlresult=[htmlresult stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];
        
    }

    return htmlresult;
    
}

- (NSString *)filterHtmlTag_Xxgknb:(NSString *)originHtmlStr{
    NSString *result = [[[NSString alloc]init]autorelease];
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];
        //        NSLog(@"start-> %d   end-> %d", arrowTagStartRange.location, arrowTagEndRange.location);
        //        NSString *arrowSubString = [originHtmlStr substringWithRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location)];
        result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
        // NSLog(@"Result--->%@", result);
        return [self filterHtmlTag:result];    //递归，过滤下一个标签
    }else{
        result = [originHtmlStr stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];  // 过滤&nbsp等标签
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........
        
    }
    return result;
}

@end
